﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using zichan.DAL;
using zichan.Entity;
using MvcPaging;
namespace zichan.BLL
{
    public class RepertoryService
    {
        public static Asset Get(int id)
        {
            using (ZichanDBContext db = new ZichanDBContext())
            {
                Asset model = db.Assets.FirstOrDefault(o => o.Id == id);
                model.AssetClassName = AsseteClassService.GetAsseteClass(model.AssetClassId).Name;
                model.CompanyName = CompanyClassService.GetCompany(model.CompanyId).Name;
                model.BuyerName = StaffService.Get(model.BuyerId).DepartmentName + "-" + StaffService.Get(model.BuyerId).Name;
                model.RecipientName = StaffService.Get(model.Recipient).DepartmentName + "-" + StaffService.Get(model.Recipient).Name;
                return model;
            }
        }
        public static IPagedList<Asset> List(string code, string userName, string name, string remarkKeyword, string adminName, int? assetClassId,int? serverPurposeId, int? companyId, int? statu, DateTime? sdt, DateTime? edt, int pageIndex, int pageSize)
        {
            using (ZichanDBContext db = new ZichanDBContext())
            {
                IQueryable<Asset> asset = db.Assets.Where(o => o.Status > 0);
                if (!string.IsNullOrEmpty(code))
                {
                    asset = asset.Where(o => o.Code.Contains(code));
                }
                if (!string.IsNullOrEmpty(userName))
                {
                    List<int> staffIds = db.Staffs.Where(o => o.Name.Contains(userName)).Select(o => o.Id).ToList();
                    asset = asset.Where(o => staffIds.Contains(o.UserId.Value));
                }
                if (!string.IsNullOrEmpty(name))
                {
                    asset = asset.Where(o => o.Name.Contains(name));
                }
                if (!string.IsNullOrEmpty(remarkKeyword))
                {
                    List<int> assetIds = db.UsageLogs.Where(o => o.Remark.Contains(remarkKeyword)).Select(o => o.AssetId).ToList();
                    asset = asset.Where(o => assetIds.Contains(o.Id));
                }
                if (!string.IsNullOrEmpty(adminName))
                {
                    List<int> staffIds = db.Staffs.Where(o => o.Name.Contains(adminName)).Select(o => o.Id).ToList();
                    List<int> assetIds = db.ServerInfos.Where(o => staffIds.Contains(o.AdminiStratorId.Value)).Select(o => o.AssetId).ToList();
                    asset = asset.Where(o => assetIds.Contains(o.Id));
                }
                if (assetClassId.HasValue)
                {
                    asset = asset.Where(o => o.AssetClassId == assetClassId.Value);
                }
                if (serverPurposeId.HasValue)
                {
                    List<string> codes = db.PutawayServers.Where(o => o.serverPurposeId == serverPurposeId).Select(o => o.Code).ToList();
                    asset = asset.Where(o => codes.Contains(o.Code));
                }
                if (companyId.HasValue)
                {
                    asset = asset.Where(o => o.CompanyId == companyId.Value);
                }
                if (statu.HasValue)
                {
                    asset = asset.Where(o => o.Status == statu);
                }
                if (sdt.HasValue)
                {
                    asset = asset.Where(o => o.BuyTime >= sdt);
                }
                if (edt.HasValue)
                {
                    edt = DateTime.Parse(string.Format("{0:yyyy-MM-dd 23:59:59}", edt));
                    asset = asset.Where(o => o.BuyTime <= edt);
                }
                List<Asset> list = asset.OrderByDescending(o => o.StatusTime).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
                List<int> assetClassIds = list.Select(o => o.AssetClassId).Distinct().ToList();
                var assetClassNames = db.AsseteClassifies.Where(o => assetClassIds.Contains(o.Id)).Select(o => new { o.Id, o.Name });
                List<int> companyIds = list.Select(o => o.CompanyId).Distinct().ToList();
                var companyNames = db.CompanyClasses.Where(o => companyIds.Contains(o.Id)).Select(o => new { o.Id, o.Name });
                List<int> userIds = list.Where(o => o.UserId.HasValue).Select(o => o.UserId.Value).Distinct().ToList();
                var userNames = db.Staffs.Where(o => userIds.Contains(o.Id)).Select(o => new { o.Id, o.Name, o.DepartmentName });

                foreach (Asset a in list)
                {
                    a.AssetClassName = assetClassNames.FirstOrDefault(o => o.Id == a.AssetClassId).Name;
                    a.CompanyName = companyNames.FirstOrDefault(o => o.Id == a.CompanyId).Name;
                    if (a.UserId.HasValue)
                    {
                        a.UserName = userNames.FirstOrDefault(o => o.Id == a.UserId).DepartmentName + "-" + userNames.FirstOrDefault(o => o.Id == a.UserId).Name;
                    }
                   
                }
                return list.OrderByDescending(o => o.StatusTime).ToPagedList(pageIndex - 1, pageSize, asset.Count());
            }
        }
        public static List<Asset> List(string code, string userName, string name, string remarkKeyword, string adminName, int? assetClassId, int? serverPurposeId, int? companyId, int? statu, DateTime? sdt, DateTime? edt)
        {
            using (ZichanDBContext db = new ZichanDBContext())
            {
                IQueryable<Asset> asset = db.Assets.Where(o => o.Status > 0);
                if (!string.IsNullOrEmpty(code))
                {
                    asset = asset.Where(o => o.Code.Contains(code));
                }
                if (!string.IsNullOrEmpty(userName))
                {
                    List<int> staffIds = db.Staffs.Where(o => o.Name.Contains(userName)).Select(o => o.Id).ToList();
                    asset = asset.Where(o => staffIds.Contains(o.UserId.Value));
                }
                if (!string.IsNullOrEmpty(name))
                {
                    asset = asset.Where(o => o.Name.Contains(name));
                }
                if (!string.IsNullOrEmpty(remarkKeyword))
                {
                    List<int> assetIds = db.UsageLogs.Where(o => o.Remark.Contains(remarkKeyword)).Select(o => o.AssetId).ToList();
                    asset = asset.Where(o => assetIds.Contains(o.Id));
                }
                if (!string.IsNullOrEmpty(adminName))
                {
                    List<int> staffIds = db.Staffs.Where(o => o.Name.Contains(adminName)).Select(o => o.Id).ToList();
                    List<int> assetIds = db.ServerInfos.Where(o => staffIds.Contains(o.AdminiStratorId.Value)).Select(o => o.AssetId).ToList();
                    asset = asset.Where(o => assetIds.Contains(o.Id));
                }
                if (assetClassId.HasValue)
                {
                    asset = asset.Where(o => o.AssetClassId == assetClassId.Value);
                }
                if (serverPurposeId.HasValue)
                {
                    List<string> codes = db.PutawayServers.Where(o => o.serverPurposeId == serverPurposeId).Select(o => o.Code).ToList();
                    asset = asset.Where(o => codes.Contains(o.Code));
                }
                if (companyId.HasValue)
                {
                    asset = asset.Where(o => o.CompanyId == companyId.Value);
                }
                if (statu.HasValue)
                {
                    asset = asset.Where(o => o.Status == statu);
                }
                if (sdt.HasValue)
                {
                    asset = asset.Where(o => o.BuyTime >= sdt);
                }
                if (edt.HasValue)
                {
                    edt = DateTime.Parse(string.Format("{0:yyyy-MM-dd 23:59:59}", edt));
                    asset = asset.Where(o => o.BuyTime <= edt);
                }
                List<int> assetClassIds = asset.Select(o => o.AssetClassId).Distinct().ToList();
                var assetClassNames = db.AsseteClassifies.Where(o => assetClassIds.Contains(o.Id)).Select(o => new { o.Id, o.Name });
                List<int> companyIds = asset.Select(o => o.CompanyId).Distinct().ToList();
                var companyNames = db.CompanyClasses.Where(o => companyIds.Contains(o.Id)).Select(o => new { o.Id, o.Name });
                List<int> userIds = asset.Where(o => o.UserId.HasValue).Select(o => o.UserId.Value).Distinct().ToList();
                var userNames = db.Staffs.Where(o => userIds.Contains(o.Id)).Select(o => new { o.Id, o.Name, o.DepartmentName });
                foreach (Asset a in asset)
                {
                    a.AssetClassName = assetClassNames.FirstOrDefault(o => o.Id == a.AssetClassId).Name;
                    a.CompanyName = companyNames.FirstOrDefault(o => o.Id == a.CompanyId).Name;
                    if (a.UserId.HasValue)
                    {
                        a.UserName = userNames.FirstOrDefault(o => o.Id == a.UserId).DepartmentName + "-" + userNames.FirstOrDefault(o => o.Id == a.UserId).Name;
                    }
                    if (a.Status == 1)
                    {
                        a.StatusName = "库存";
                    }
                    if (a.Status == 2)
                    {
                        a.StatusName = "使用中";
                    }
                    if (a.Status == 3)
                    {
                        a.StatusName = "报废";
                    }
                }
                return asset.OrderByDescending(o => o.StatusTime).ToList();
            }
        }
        /// <summary>
        /// 出库
        /// </summary>
        /// <returns></returns>
        public static string OutStorage(Asset model)
        {
            using (ZichanDBContext db = new ZichanDBContext())
            {
                try
                {
                    db.Entry(model).State = System.Data.Entity.EntityState.Modified;
                    db.SaveChanges();
                    return string.Empty;
                }
                catch (Exception ex)
                {
                    return "无法出库";
                }
            }
        }
        public static void Count(out int sCount,out int spareCount,out int busyCount,out int scrapCount)
        {
            using (ZichanDBContext db = new ZichanDBContext())
            {
                IQueryable<Asset> w = db.Assets.Where(o => o.AssetClassId == 1&&o.Status>0);
                sCount = w.Count();
                spareCount = w.Where(o => o.Status == 1).Count();
                busyCount = w.Count(o => o.Status == 2);
                scrapCount = w.Count(o => o.Status == 3);
            }
        }
    }
}
